// pages/login/login.js
var app = getApp()
var url = app.globalData.url
var uid = 0
var countdown = 60; 
var that = "";

function settime() {
  if (countdown == 0) {
    that.setData({
      btnMsg: "获取验证码",
      disabled : false
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      btnMsg: "重新发送(" + countdown + ")",
      disabled: true
    })
    countdown--;
  }
  setTimeout(function () {
    settime()
  }, 1000)
}

Page({
  data: {
    phone: '',
    password: '',
    btnColor:'',
    btnMsg : '获取验证码',
    disabled : false
  },
  onLoad:function(){
    that = this
    wx.getStorage({
      key: 'uid',
      success: function (res) {
        console.log('uid-------' + res.data)
        uid = res.data
      },
    })
  },

  // 获取输入账号 
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  userInfoHandler: function(data){
    var avatarUrl = ''
    var nikname = ''
    var sex = ''
    var pro = ''
    var city = ''
    var con = ''
   
    console.log(data)
    if (data.detail.errMsg == 'getUserInfo:ok'){
        avatarUrl = data.detail.userInfo.avatarUrl,
        nikname = data.detail.userInfo.nickName,
        sex = data.detail.userInfo.gender,
        pro = data.detail.userInfo.province,
        city = data.detail.userInfo.city
        con = data.detail.userInfo.country

        this.login(avatarUrl, nikname, sex, pro, city, con)
    }else{
      wx.showToast({
        title: '未获取微信用户信息，请重试',
        icon: 'loading',
        duration: 2000
      })
    }

    
   
  },

  // 登录 
  login: function (avatarUrl, nikname, sex, pro, city, con) {
    console.log(9999999999)
    if (this.data.phone.length == 0 || this.data.password.length == 0) {
      wx.showToast({
        title: '用户名或验证码不能为空',
        icon: 'loading',
        duration: 2000
      })
    } else {
      console.log(this.data.phone)
      console.log(this.data.password)
      wx.request({
        url: url+'/bindPhone',
        method: 'POST',
        header: {
          //设置参数内容类型为x-www-form-urlencoded
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        data: {
          sPhone: this.data.phone,
          code:this.data.password,
          uWxImg: avatarUrl, 
          uWxNc: nikname, 
          uSex:sex, 
          uProvince:pro,
          uCity:city,
          uCountry:con,
          uid:uid
        },
        success: function (res) {
          console.log(res)
          var status = res.data.status;
          console.log(status)
          if(status != 1){
            wx.showToast({
              title: res.data.msg,
              icon: 'loading',
              duration: 2000
            })
          }else{
            wx.switchTab({
              url: '/pages/index/index',
            })
            // 这里修改成跳转的页面 
            /*wx.request({
              url: url + '/regAndLg',
              method: 'POST',
              header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/x-www-form-urlencoded',
                'Accept': 'application/json'
              },
              data: {
                uid: uid,
                uPhone: this.data.phone,
                uWxImg: app.globalData.userInfo.avatarUrl,
                uWxNc: app.globalData.userInfo.nickName,
                uSex: app.globalData.userInfo.gender,
                uCity: app.globalData.userInfo.city,
                uProvince: app.globalData.userInfo.province,
                uCountry: app.globalData.userInfo.country
              },
              success: function (res) {
                var state = res.data.status;
                if(state == 0){
                  wx.showToast({
                    title: '注册失败',
                    icon: 'success',
                    duration: 2000
                  })
                }else{
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.navigateTo({
                    url: '../map/map',
                  })
                }
              }
            })*/
          }
        }
      })
    }



  },
  getCode:function(){
    

    var phonenum = this.data.phone
    if(phonenum == null || phonenum == ''){
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 1000
      })

      return
    }
    wx.request({
      url: url+'/sendCode',
      method: 'POST',
      header: {
        //设置参数内容类型为x-www-form-urlencoded
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      data: {
        sPhone:phonenum
      },
      success:function(res){
        console.log(res)
        let state = res.data.status
        if(state == 1){
          settime();
          wx.showToast({
            title: '验证码获取成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  }
})